<?php
/*
 * 门店
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Store;
use app\adminbackstage\model\User;
use think\facade\Db;

class StoreController extends BaseController{

    //门店列表
    public function storeList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $store_name = Request::param('store_name');
        $enable= Request::param('enable');
//        $type = Request::param('type');

        $where = [];
        if(!empty($enable==-1)){
            $where[] = ['enable','=',0];
        }
        else{
             $where[] = ['enable','=','1'];
        }
       
        if(!empty($store_name)){
            $where[] = ['store_name','like','%'.$store_name.'%'];
        }
        
//        if(!empty($w_name)){
//            $where[] = ['w.w_name','like','%'.$w_name.'%'];
//        }
//        if(!empty($type)){
//            $where[] = ['type','=',$type];
//        }
        $list = Store::storeList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Store::storeCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //门店添加
    public function storeAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $store_name = Request::param('store_name');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        $business_hours_min = Request::param('business_hours_min');
        $business_hours_max = Request::param('business_hours_max');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [
            'store_name' => $store_name,
            'business_hours_min' => $business_hours_min,
            'business_hours_max' => $business_hours_max,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'enable' => $enable,
            'addtime' => $time,
        ];

        Store::storeAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //门店详情
    public function storeInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Store::storeInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //门店修改
    public function storeEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $store_name = Request::param('store_name');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        $business_hours_min = Request::param('business_hours_min');
        $business_hours_max = Request::param('business_hours_max');
        
        $update = [];
        $update_s = [];
        $where_s = [];
        if(!empty($store_name)){
            $where = [];
            $where[] = ['store_name','=',$store_name];

            /*$info = Store::storeInfo($where);
            if(!empty($info)){
                return json(['code' => 0, 'msg' => lang('店铺名已存在'), 'data' => '', 'count' => '']);
            }*/ 
            $update['store_name'] = $store_name;
            
            $where_s[] = ['stid','=',$id];
            $update_s['store_name'] = $store_name;
        }
        
        if(!empty($business_hours_min)){
            $update['business_hours_min'] = $business_hours_min;
        }
        
        if(!empty($business_hours_max)){
            $update['business_hours_max'] = $business_hours_max;
        }
        
        if(!empty($province)){
            $update['province'] = $province;
        }
        
        if(!empty($city)){
            $update['city'] = $city;
        }
        
        if(!empty($district)){
            $update['district'] = $district;
        }
        
        if(!empty($address)){
            $update['address'] = $address;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }
        
        Db::startTrans();//开启事务
        Store::storeEdit($id,$update);
        if(!empty($where_s)){
            $user = new User();
            $user->userUpdateAll($where_s,$update_s);
        }
        Db::commit();//事务提交
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
